package com.xy.bizportdemo.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.xy.bizportdemo.database.base.DBHelper;
import com.xy.bizportdemo.database.entity.SmsDBTableConversation;
import com.xy.bizportdemo.database.entity.SmsDBTableMessage;

/**
 * Created by XIAOYUAN on 2016/11/23.
 */

public class SmsDBHelper extends DBHelper {
    private static final String TAG = "SmsDBHelper";

    public static final String DATABASE_NAME = "sms.db";
    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_AUTHORITY = "com.xy.bizportdemo";

    public static final Uri URI_MESSAGE = Uri.parse("content://" + DATABASE_AUTHORITY + "/" + SmsDBTableMessage.NAME);
    public static final Uri URI_CONVERSATION = Uri.parse("content://" + DATABASE_AUTHORITY + "/" + SmsDBTableConversation.NAME);

    private static SmsDBHelper sSmsDBHelper = null;

    public static SmsDBHelper getInstance(Context context) {
        synchronized (SmsDBHelper.class) {
            if (sSmsDBHelper == null) {
                sSmsDBHelper = new SmsDBHelper(context);
            }
        }

        return sSmsDBHelper;
    }

    private SmsDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        sTableNames[1] = SmsDBTableMessage.NAME;
        sTableNames[2] = SmsDBTableConversation.NAME;

        sMatcher.addURI(DATABASE_AUTHORITY, sTableNames[1], 1);
        sMatcher.addURI(DATABASE_AUTHORITY, sTableNames[2], 2);
    }

    @Override
    protected void createTable(SQLiteDatabase db) {
        db.execSQL(SmsDBTableMessage.CREATE_TABLE);
        db.execSQL(SmsDBTableConversation.CREATE_TABLE);
    }

    @Override
    protected void dropTable(SQLiteDatabase db) {
        db.execSQL(SmsDBTableMessage.DROP_TABLE);
        db.execSQL(SmsDBTableConversation.DROP_TABLE);
    }
}
